﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ajeelee.Core.Utils;
using AiLuWeb.Business;
using AiLuWeb.Model;
using AiLuWeb.WebSite.Code;

namespace AiLuWeb.WebSite.WebAdmin
{
    public partial class JobEdit : AuthPageBase
    {
        protected int id;
        protected Job info = new Job();
        protected IList<NewsClass> teamClassList = new List<NewsClass>();
        IJobBusiness jobBll = new JobBusinessFactory().CreateJobBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                teamClassList = Utility.GetNewsClass().Where(c => c.PId == 12).ToList();

                id = QueryParamInt32("id");
                info = jobBll.GetById(id);
                if (IsPost)
                {
                    if (id > 0 && info != null)   // 更新
                    {
                        InitInfo();
                        jobBll.Update(info);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("JobList.aspx", true);

                    }
                    else          // 新建
                    {
                        info = new Job();
                        InitInfo();
                        info.Author = User.Identity.Name;
                        info.InDate = DateTime.Now;
                        info.InIP = IPAddress;

                        jobBll.Insert(info);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("JobList.aspx", true);
                    }
                }
                else
                {
                    if (info == null)
                    {
                        info = new Job();
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }

        private void InitInfo()
        {
            info.Cid = FormParamInt32("Cid");
            info.Name = FormParam("Name");
            info.NeedNum = FormParamInt32("NeedNum");
            info.Shalary = FormParamInt32("Shalary");
            info.WorkingPlace = FormParam("WorkingPlace");
            info.RequiredDegree = FormParamInt32("RequiredDegree");
            info.Sex = FormParamInt32("Sex");
            info.Department = FormParam("Department");
            info.WorkExperience = FormParamInt32("WorkExperience");
            info.EffectiveDate = FormParam("EffectiveDate").IsNotNullAndEmpty() ? FormParam("EffectiveDate").ToDateTime().ToString("yyyy-MM-dd") : "";
            info.Description = FormParam("Description");

            info.UpAuthor = User.Identity.Name;
            info.UpDate = DateTime.Now;
            info.UpIP = IPAddress;
        }
    }
}